home *** CD-ROM | disk | FTP | other *** search
- // -----------------------------------------------------------------------------
- // File : demoCDEF.c
- // Date : August 24, 1994
- // Author : Jim Stout
- // Purpose : A demonstration routine for :
- // - my collection of CDEF's
- // - dialogAssist.c dialog utilities
- // - panelAssist.c tabPanel CDEF handling
- // - movableModal.c dialog routines
- //
- // Note the special usage of dim text routines required when using
- // AppendDITL and ShortenDITL routines. See file dimText.c.
- //
- // This demo uses the "TabPanel" CDEF - see "About CDEF's╔".
- //
- // -----------------------------------------------------------------------------
- #include <GestaltEqu.h>
- #include <stdio.h>
-
- #include "demoCDEF.h"
- #include "jimsCDEF.h"
- #include "dialogAssist.h"
- #include "dimText.h"
- #include "panelAssist.h"
- #include "movableModal.h"
- #include "TogLib.h"
-
- #if THINK_C == 7
- #define ProcPtr ControlActionUPP
- #endif
-
- // -----------------------------------------------------------------------------
- // temporary storage for control values
- // -----------------------------------------------------------------------------
- short buttonValue[14];
- short popUpValue[4];
- short spinnerValue[6];
- long dateValue[6];
- short hSliderValue[5];
- short vSliderValue[6];
- short progressValue[8];
-
- // -----------------------------------------------------------------------------
- // local routines
- // -----------------------------------------------------------------------------
-
- static void demoPanel (void);
- static void processPanel (DialogPtr theDialog, short currPanel,
- short itemHit);
- static void savePanel (DialogPtr theDialog, short toPanel);
- static void restorePanel (DialogPtr theDialog, short toPanel);
- static pascal char filter (DialogPtr theDialog, EventRecord *theEvent,
- short *theItem);
- static pascal void trackSpin (ControlHandle c, short p);
- static pascal void trackSlider (ControlHandle c, short p);
- static pascal void trackSpin6 (ControlHandle c, short p);
- static void setNewFont (DialogPtr theDialog, short newFont,
- long newSize);
- static void setNewSize (DialogPtr theDialog, long newSize);
-
- // -----------------------------------------------------------------------------
- // routine called by movableModalDialog, replace with a real one╔
- // -----------------------------------------------------------------------------
-
- extern void updateWindow(WindowPtr);
-
- extern void updateWindow(WindowPtr w)
- {
- SetPort(w);
- BeginUpdate(w);
- EndUpdate(w);
- }
-
- // -----------------------------------------------------------------------------
- // Mainline starts here :)
- // -----------------------------------------------------------------------------
-
- main()
- {
- InitGraf(&thePort);
- InitWindows();
- InitFonts();
- InitMenus();
- TEInit();
- InitCursor();
- InitDialogs(0L);
-
- if(!daGestalt(gestaltDITLExtAttr))
- StopAlert(256, nil);
- else
- demoPanel();
- }
-
- // -----------------------------------------------------------------------------
- // demo the tab dialog panel
- // -----------------------------------------------------------------------------
- static void demoPanel()
- {
-
- DialogPtr theDialog;
- short itemHit, inx, currPanel=0,toPanel;
- Handle h;
- Rect r;
- Boolean dimIt = true;
-
- theDialog = GetNewDialog(128,0L,(DialogPtr)-1);
- if(theDialog) {
-
- SetPort(theDialog);
- initDimText(theDialog); // set up for dimmable text
-
- // initialize the dialog to the correct panel
-
- toPanel = daGetCtlValue(theDialog, TABCNTL);
-
- disposeDimData(theDialog);
- panelSwap(theDialog, currPanel, toPanel, DITLBASE);
- makeDimmable(theDialog);
-
- // a "real" application would probably get control settings from some sort
- // of configuration file/record/structure. We'll just initialize our stuff
- // by calling savePanel() which will take the values from the CNTL templates.
-
- savePanel(theDialog, toPanel);
-
- // restore control settings and initialize the panel.
-
- restorePanel(theDialog, toPanel);
- currPanel = toPanel;
-
- // show the dialog and start handling events
-
- ShowWindow(theDialog);
-
- do {
-
- movableModalDialog ((filterProc)filter,&itemHit);
-
- // a click on a tab, change to that panel
-
- if(itemHit == TABCNTL) {
-
- toPanel = daGetCtlValue(theDialog, TABCNTL);
-
- if(toPanel != currPanel) {
-
- // save settings of current panel first
-
- savePanel(theDialog, currPanel);
-
- // switch to the new panel and restore/initialize it
-
- disposeDimData(theDialog);
- panelSwap(theDialog, currPanel, toPanel, DITLBASE);
- makeDimmable(theDialog);
-
- restorePanel(theDialog, toPanel);
-
- currPanel = toPanel;
-
- // change the Disable/Enable button if needed
-
- if(!dimIt) {
- daDimOne(theDialog, DISABLE, false);
- daSetCtlTitle(theDialog, DISABLE, "\pDisable");
- dimIt = true;
- }
- }
- }
- else
-
- // show what the disabled controls look like
-
- if(itemHit == DISABLE) {
- if(dimIt)
- daSetCtlTitle(theDialog, DISABLE, "\pEnable");
- else
- daSetCtlTitle(theDialog, DISABLE, "\pDisable");
- inx = CountDITL(theDialog);
- daDimItems(theDialog, DITLBASE+1, inx, dimIt);
- dimIt = !dimIt;
- if(currPanel == 1)
- daDimOne(theDialog, PB1, true);
- }
- else
-
- // handle a click on a control within a panel
-
- processPanel(theDialog, currPanel, itemHit);
-
- }while(itemHit != ok && itemHit != cancel);
-
- if(itemHit == OK) {
- // savePanel(theDialog, currPanel);
- // some code to apply the temporary storage control values
- // in your application could go here.
- }
- disposeDimText(theDialog);
- DisposDialog(theDialog);
- }
- }
-
- // -----------------------------------------------------------------------------
- // Handle a click on a control
- // -----------------------------------------------------------------------------
- static void processPanel(DialogPtr theDialog, short currPanel, short itemHit)
- {
- ControlHandle h;
- short t, val, newFont;
- Rect r;
- long num, newSize;
- Str255 fontName,fontSize;
- unsigned long secs;
- popUpPrivateDataH hPpd;
-
- switch (currPanel) {
- case BUTTONS:
- switch(itemHit) {
- case CB1: // click on a checkBox
- case CB2:
- case CB3:
- case CB4:
- daToggleCheck(theDialog, itemHit);
- resetTogButtons(theDialog,TB1);
- break;
- case RB1: // click on a radioButton
- case RB2:
- case RB3:
- case RB4:
- daToggleRadio(theDialog, itemHit, RB1, RB4);
- resetTogButtons(theDialog,TB1);
- break;
- case TB1: // click on a TogButton
- case TB2:
- case TB3:
- case TB4:
- setTogButtons(theDialog, itemHit, TB1, TB4);
- break;
- case EDITSIZE: // keydown in edit text
- num = daGetINum(theDialog, EDITSIZE);
- setNewSize(theDialog, num);
- resetTogButtons(theDialog,TB1);
- break;
- case SIZEPOP:
- GetDItem(theDialog,SIZEPOP,&t,(Handle *)&h,&r);
- hPpd = (popUpPrivateDataH)(*h)->contrlData;
-
- newSize = GetCtlValue(h);
- GetItem((*hPpd)->mHandle, newSize, fontSize);
- daSetIText(theDialog, EDITSIZE, fontSize);
- daSelIText(theDialog, EDITSIZE);
- resetTogButtons(theDialog,TB1);
- break;
- case SETFONT:
-
- // get the font size from the popup menu
-
- GetDItem(theDialog,SIZEPOP,&t,(Handle *)&h,&r);
- val = GetCtlValue(h);
- hPpd = (popUpPrivateDataH)(*h)->contrlData;
- GetItem((*hPpd)->mHandle, val, fontSize);
- StringToNum(fontSize, &newSize);
-
- // get the font from the popup menu
-
- GetDItem(theDialog,FONTPOP,&t,(Handle *)&h,&r);
- val = GetCtlValue(h);
- hPpd = (popUpPrivateDataH)(*h)->contrlData;
- GetItem((*hPpd)->mHandle, val, fontName);
- GetFNum(fontName, &newFont);
- setNewFont(theDialog, newFont, newSize);
- resetTogButtons(theDialog,TB1);
- break;
- }
- break;
-
- // set the value of the arrow control to match the edit text item
-
- case SPINNERS:
- if(itemHit == EDITSPIN) {
- num = daGetINum(theDialog, EDITSPIN);
- daSetCtlValue(theDialog, SPIN3, (short)num);
- }
- break;
-
- // reset the date time controls if RESET button clicked
-
- case DATETIME:
- if(itemHit == RESET) {
- GetDateTime(&secs); // new date/time
- for(t=1;t<=6;t++) {
- h = daGetCtlHandle(theDialog, t+DITLBASE);
- if(h) {
- SetCRefCon(h, secs); // tell control about it
- InvalRect(&(**h).contrlRect); // force redraw
- }
- }
- }
- break;
-
- // show how a spinner control can adjust a progress bar control
-
- case PROGBARS:
- if(itemHit == SPIN10) {
- daSetCtlValue(theDialog, PROG9,
- daGetCtlValue(theDialog, SPIN10));
- }
- break;
-
- // we don't do any special processing for the other control examples.
-
- case POPUPS:
- break;
- case HSLIDERS:
- break;
- case VSLIDERS:
- break;
- }
- }
- // -----------------------------------------------------------------------------
- // A sample routine to show one method of saving the settings of the controls
- // on a panel. When a panel is "dismissed", the Dialog Manager disposes of the
- // controls on the panel, so we have to save the values to restore if the panel
- // is displayed again.
- //
- // This is pretty "brute force" - just some global arrays of control values.
- //
- // -----------------------------------------------------------------------------
- static void savePanel(DialogPtr theDialog, short currPanel)
- {
- ControlHandle h;
- Rect r;
- short t,inx;
-
- switch (currPanel) {
- case BUTTONS:
- for(inx=1;inx<=12;inx++)
- buttonValue[inx-1] = daGetCtlValue(theDialog, inx+DITLBASE);
- buttonValue[12] = daGetINum(theDialog, EDITSIZE);
- buttonValue[13] = daGetCtlValue(theDialog, FONTPOP);
- break;
- case POPUPS:
- for(inx=1;inx<=4;inx++)
- popUpValue[inx-1] = daGetCtlValue(theDialog, inx+DITLBASE);
- break;
- case SPINNERS:
- for(inx=1;inx<=6;inx++)
- spinnerValue[inx-1] = daGetCtlValue(theDialog, inx+DITLBASE);
- break;
- case DATETIME:
- for(inx=1;inx<=6;inx++)
- dateValue[inx-1] = daGetCtlRefCon(theDialog, inx+DITLBASE);
- break;
- case HSLIDERS:
- for(inx=1;inx<=5;inx++)
- hSliderValue[inx-1] = daGetCtlValue(theDialog, inx+DITLBASE);
- break;
- case VSLIDERS:
- for(inx=1;inx<=6;inx++)
- vSliderValue[inx-1] = daGetCtlValue(theDialog, inx+DITLBASE);
- break;
- case PROGBARS:
- for(inx=1;inx<=8;inx++)
- progressValue[inx-1] = daGetCtlValue(theDialog, inx+DITLBASE);
- break;
- }
- }
-
- // -----------------------------------------------------------------------------
- // A sample routine to show one method of restoring the control settings to
- // proper values when a panel is shown by clicking on its tab.
- //
- // This routine also does some special initialization of controls.
- // -----------------------------------------------------------------------------
- static void restorePanel(DialogPtr theDialog, short toPanel)
- {
- ControlHandle ch;
- SpinHandle hSpin;
- Rect r;
- short t,inx;
- long l;
-
- switch(toPanel) {
-
- case BUTTONS:
-
- // restore control values
-
- for(inx=1;inx<=12;inx++)
- daSetCtlValue(theDialog, inx+DITLBASE, buttonValue[inx-1]);
- daSetINum(theDialog, EDITSIZE, buttonValue[12]);
- setNewSize(theDialog, buttonValue[12]);
- daSetCtlValue(theDialog, FONTPOP, buttonValue[13]);
-
- // initialize the Tog buttons and disable one button
-
- initTogButtons(theDialog, TB1,TB4);
- daDimOne(theDialog, PB1, true);
- break;
-
- case POPUPS:
-
- // set font & restore control values
-
- TextFont(systemFont);
- TextSize(0);
- for(inx=1;inx<=4;inx++)
- daSetCtlValue(theDialog, inx+DITLBASE, popUpValue[inx-1]);
- break;
-
- case SPINNERS:
-
- // set font & restore control values
-
- setNewFont(theDialog, 0, 0);
-
- for(inx=1;inx<=6;inx++)
- daSetCtlValue(theDialog, inx+DITLBASE, spinnerValue[inx-1]);
-
- // initialize edit text item to the value of the 3rd spinner
-
- daSetINum(theDialog, EDITSPIN,
- daGetCtlValue(theDialog, SPIN3));
- SelIText(theDialog, EDITSPIN, 32768, 32768);
-
- // the 6th spinner will adjust by 0.10, so stash a divisor value of 10
- // in the userData field
-
- ch = daGetCtlHandle(theDialog, SPIN6);
- if(ch) {
- hSpin = (SpinHandle)(**ch).contrlData;
- (**hSpin).userData = 10;
- trackSpin6(ch, 1); // draw the value
- }
-
- break;
-
- case DATETIME:
-
- // set font & restore control values
-
- TextFont(geneva);
- TextSize(9);
- for(inx=1;inx<=6;inx++)
- daSetCtlRefCon(theDialog, inx+DITLBASE, dateValue[inx-1]);
- break;
-
- case HSLIDERS:
-
- // set font & restore control values
-
- TextFont(geneva);
- TextSize(9);
- for(inx=1;inx<=5;inx++)
- daSetCtlValue(theDialog, inx+DITLBASE, hSliderValue[inx-1]);
-
- // set a ctlAction proc for slider 3
-
- ch = daGetCtlHandle(theDialog, HSLIDER3);
- SetCtlAction(ch, (ProcPtr)trackSlider); // or (ControlActionUPP)
- daSetINum(theDialog, HSLIDERVAL, GetCtlValue(ch));
- break;
-
- case VSLIDERS:
- TextFont(geneva);
- TextSize(9);
-
- // restore control values
-
- for(inx=1;inx<=6;inx++)
- daSetCtlValue(theDialog, inx+DITLBASE, vSliderValue[inx-1]);
-
- // set a ctlAction proc for slider 5
-
- ch = daGetCtlHandle(theDialog, VSLIDER5);
- SetCtlAction(ch, (ProcPtr)trackSlider);
- daSetINum(theDialog, VSLIDERVAL, GetCtlValue(ch));
- break;
-
- case PROGBARS:
-
- // set font & restore control values
-
- TextFont(geneva);
- TextSize(9);
- for(inx=1;inx<=8;inx++)
- daSetCtlValue(theDialog, inx+DITLBASE, progressValue[inx-1]);
- break;
- }
- }
- // -----------------------------------------------------------------------------
- // a simple dialog filter proc
- // -----------------------------------------------------------------------------
- static pascal char filter(DialogPtr theDialog, EventRecord *theEvent, short *theItem)
- {
- char result = FALSE;
- char c;
- short panelNum,inx,editItem,partCode;
- long min,max;
- ControlHandle cHdl;
- Point mousePt;
-
- switch(theEvent->what) {
-
- panelNum = daGetCtlValue(theDialog, TABCNTL);
-
- case nullEvent:
-
- // for this demo program, we'll use null events to demo the progress bar
- // cdefs on panel 7 of the dialog
-
- if(panelNum == PROGBARS) {
- for(inx=DITLBASE+1;inx<=DITLBASE+8;inx++) {
- daSetCtlValue(theDialog, inx,
- daGetCtlValue(theDialog, inx)+1);
- if(daGetCtlValue(theDialog, inx) >= daGetCtlMax(theDialog, inx))
- daSetCtlValue(theDialog, inx, 0);
- }
- }
- break;
-
-
- case keyDown:
- case autoKey:
-
- // for this demo, we'll show how to use command or control key
- // combinations to operate the tabPanel CDEF
-
- c = theEvent->message & charCodeMask;
-
- if(theEvent->modifiers & cmdKey) {
- if(result = panelCmdKey(theDialog, TABCNTL, c, theItem))
- break;
- }
- if(theEvent->modifiers & controlKey ||
- theEvent->modifiers & cmdKey) {
- if(result = panelCmdTab(theDialog, TABCNTL, c, theItem))
- break;
- }
- if(result = daExitKey(theDialog, theEvent, theItem, cancel))
- break;
-
- // handle a forward delete key
-
- if(result = daForwardDel(theDialog, c))
- break;
- if(theEvent->modifiers & shiftKey) {
- if(result = daShiftSelect(theDialog, c))
- break;
- }
- editItem = ((DialogPeek)theDialog)->editField + 1;
-
- // limit to numeric entry only
-
- if(panelNum == BUTTONS && editItem == EDITSIZE) {
- result = daEnterNumber(theDialog, editItem, 1L, 18L, c);
- }
- else
- if(panelNum == SPINNERS) {
- max = daGetCtlMax(theDialog, SPIN3);
- min = daGetCtlMin(theDialog, SPIN3);
- result = daEnterNumber(theDialog, editItem, min, max, c);
- }
- break;
-
- case mouseDown:
- mousePt = theEvent->where;
- GlobalToLocal(&mousePt);
- partCode = FindControl(mousePt, theDialog, &cHdl);
-
- // demonstrate use of FindControl/TrackControl within a dialog and to show
- // how to continuously update the progress bar (item #10) from the value
- // of the spinner (item #9)
-
- if(panelNum == PROGBARS) {
- if(partCode && cHdl == daGetCtlHandle(theDialog, SPIN10)) {
- TrackControl(cHdl, mousePt, (ProcPtr)trackSpin);
- }
- }
- else
-
- // have spinner 6 is a control that adjusts by a fractional value
-
- if(panelNum == SPINNERS) {
- if(partCode && cHdl == daGetCtlHandle(theDialog, SPIN6)) {
- TrackControl(cHdl, mousePt, (ProcPtr)trackSpin6);
- }
- }
-
- break;
- }
- return result;
- }
- //----------------------------------------------------------------------------------
- // A simple action proc for call to TrackControl above. Just sets the value
- // of the progress bar to match the spinner.
- //----------------------------------------------------------------------------------
-
- static pascal void trackSpin(ControlHandle theControl, short partCode)
- {
- DialogPtr theDialog = (**theControl).contrlOwner;
-
- if(partCode) {
- daSetCtlValue(theDialog, PROG9, GetCtlValue(theControl));
- }
- }
- //----------------------------------------------------------------------------------
- // This routine is set as an actionProc via SetCtlAction to provide
- // "live" display of the values of a Slider control
- //----------------------------------------------------------------------------------
-
- static pascal void trackSlider(ControlHandle theControl, short partCode)
- {
- DialogPtr theDialog = (**theControl).contrlOwner;
-
- if(partCode) {
- if(theControl == daGetCtlHandle(theDialog, VSLIDER5))
- daSetINum(theDialog, VSLIDERVAL, GetCtlValue(theControl));
- else
- if(theControl == daGetCtlHandle(theDialog, HSLIDER3))
- daSetINum(theDialog, HSLIDERVAL, GetCtlValue(theControl));
- }
- }
- //----------------------------------------------------------------------------------
- // This Action proc shows how to have a spinner control with a fractional
- // increment
- //----------------------------------------------------------------------------------
-
- static pascal void trackSpin6(ControlHandle theControl, short partCode)
- {
- DialogPtr theDialog;
- short val,div;
- ControlHandle hCtrl;
- SpinHandle hSpin;
- Str255 s;
- double dbl;
-
- if(!partCode) // avoid flicker
- return;
-
- theDialog = (**theControl).contrlOwner;
-
- // grab the handle to the control, extract the userData value
- // to use as divisor to calculate the value to display in the text item
-
- hCtrl = daGetCtlHandle(theDialog, SPIN6);
- if(theControl == hCtrl) {
- hSpin = (SpinHandle)(**theControl).contrlData;
- div = (**hSpin).userData;
-
- val = GetCtlValue(hCtrl);
- dbl = (double)val/div;
-
- sprintf((char *)s, "%3.1lf", dbl);
- CtoPstr((char *)s);
-
- // display the calcuated value (in this case, via a statText item)
-
- daSetIText(theDialog, FRACTSPIN, s);
- }
-
- }
- //----------------------------------------------------------------------------------
- // Set a new font for this dialog. This is a nasty way to do this,
- // but it appears to work.
- //----------------------------------------------------------------------------------
-
- static void setNewFont(DialogPtr theDialog, short newFont, long newSize)
- {
- FontInfo fInfo;
-
- // set new font & size for current port
-
- TextFont(newFont);
- TextSize((short)newSize);
-
- // set new font for TEHandle in dialog (this is a crude hack!)
-
- GetFontInfo(&fInfo);
- (*((DialogPeek)theDialog)->textH)->txFont = newFont;
- (*((DialogPeek)theDialog)->textH)->txSize = newSize;
- (*((DialogPeek)theDialog)->textH)->fontAscent = fInfo.ascent;
-
- // redraw the dialog in the new font & size
-
- DrawDialog(theDialog);
- }
-
- //----------------------------------------------------------------------------------
- // Set a new size into the popup menu for font size. Called when the
- // EDITSIZE dialog edit item is changed, will add or remove a "custom" size
- // and a separator line as required.
- //----------------------------------------------------------------------------------
- static void setNewSize (DialogPtr theDialog, long newSize)
- {
- short type,val,inx,max;
- long longVal;
- ControlHandle h;
- MenuHandle hMenu;
- Rect r;
- popUpPrivateDataH hPpd;
- Str255 s;
- Boolean found=false;
-
- GetDItem(theDialog,SIZEPOP,&type,(Handle *)&h,&r); // get menu handle
- hPpd = (popUpPrivateDataH)(*h)->contrlData;
- hMenu = (*hPpd)->mHandle;
-
- val = GetCtlValue(h); // uncheck current item
- CheckItem(hMenu, val, FALSE);
-
- max = CountMItems(hMenu); // number of menu items
-
- for(inx=1;inx<=max;inx++) { // see if typed in value
- GetItem(hMenu, inx, s); // matches a menu item
- StringToNum(s, &longVal);
- if(longVal == newSize) { // yep, got a match but it is
- if(inx > 2 & max > MAXFONTSIZES) { // not custom, so let's
- DelMenuItem(hMenu,1); // delete the custom setting
- DelMenuItem(hMenu,1); // and the separator line
- inx-=2;
- }
- CheckItem(hMenu, inx, TRUE); // check new item
- SetCtlValue(h, inx);
- found = true;
- break;
- }
- }
- if(!found) { // no match, set custom item
- NumToString(newSize, s);
- if(max == MAXFONTSIZES) { // add a custom value
- InsMenuItem(hMenu, "\p(-", 0); // first, a separator
- InsMenuItem(hMenu, s, 0); // now, the new size
- }
- else {
- SetItem(hMenu, 1, s); // replace existing custom
- }
- CheckItem(hMenu, 1, TRUE);
- SetCtlValue(h, 1);
- }
- }